package stsj.meddroid.screen;

import stsj.meddroid.MainAppListener;
import stsj.meddroid.Resources;
import stsj.meddroid.Settings;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actors.Button;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.badlogic.gdx.scenes.scene2d.actors.Button.ClickListener;

public class RoundSelectScreen implements Screen, InputProcessor {

	private MainAppListener mainListener;
	private SpriteBatch batch;
	
	private Camera camera;
	
	public int currentStage;
	
	private Stage stage;
	private Image bg;

	private Button round01;
	private Button round02;
	private Button round03;
	private Button round04;
	private Button round05;

//	private Button round[];
	
	
	private Button round02U;
	private Button round03U;
	private Button round04U;
	private Button round05U;

	private boolean round01Unlocked;
	private boolean round02Unlocked;
	private boolean round03Unlocked;
	private boolean round04Unlocked;
	private boolean round05Unlocked;

	private boolean loading = false;

	private Button backButton;

	public RoundSelectScreen(MainAppListener mainListener) {
		// TODO Auto-generated constructor stub
		this.mainListener = mainListener;
		camera = Resources.camera;
		camera.position.set(240, 160, 0);
		
		stage = new Stage(480, 320, true);
		batch= Resources.batch;
		bg = new Image("Stage Background", Resources.bgStageSelectRegion);

		round01 = new Button("Stage 01 Button", Resources.stage01Region[1],
				Resources.stage01Region2[1]);
		round01.x = stage.width() * 0.20f;
		round01.y = stage.height() * 0.60f;
		
		round02 = new Button("Stage 02 Button", Resources.stage01Region[2],
				Resources.stage01Region2[2]);
		round02.x = stage.width() * 0.40f + 15;
		round02.y = stage.height() * 0.60f;

		round03 = new Button("Stage 03 Button",Resources.stage01Region[3],
				Resources.stage01Region2[3]);
		round03.x = stage.width() * 0.60f + 30;
		round03.y = stage.height() * 0.60f;

		round04 = new Button("Stage 04 Button", Resources.stage01Region[4],
				Resources.stage01Region2[4]);
		round04.x = stage.width() * 0.20f;
		round04.y = stage.height() * 0.25f;

		round05 = new Button("Stage 05 Button", Resources.stage01Region[5],
				Resources.stage01Region2[5]);
		round05.x = stage.width() * 0.40f + 15;
		round05.y = stage.height() * 0.25f;

		round01.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
//				Gdx.app.log("click start story", "a");
				if (currentStage == 1){
					setState(MainAppListener.STORY_1);
				}else if (currentStage == 2){
					setState(MainAppListener.STORY_2);
				}else if (currentStage == 3){
					setState(MainAppListener.STORY_3);
				}else if (currentStage == 4){
					setState(MainAppListener.STORY_4);
				}else if (currentStage == 5){
					setState(MainAppListener.STORY_5);
				}
				
//				Settings.unlockRound(currentStage, 2);
//				Gdx.app.log("click start story", "selesai");
			}

		};

		round02.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
//				Gdx.app.log("click start battle", "a");
				startLoading(2);
//				Gdx.app.log("click start", "selesai");
			}

		};

		round03.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
//				Gdx.app.log("click start battle", "a");
				startLoading( 3);
//				Gdx.app.log("click start", "selesai");
			}

		};

		round04.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
//				Gdx.app.log("click start battle", "a");
//				startBattle(4);
				startLoading( 4);
//				Gdx.app.log("click start", "selesai");
			}

		};

		round05.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.clickSound);
//				Gdx.app.log("click start battle", "a");
//				startBattle(5);
				if (currentStage == 5){
					setState(MainAppListener.STORY_6);
				}else{
					startLoading( 5);
				}
//				Gdx.app.log("click start", "selesai");
			}

		};

		round02U = new Button("Stage 02 Button U", Resources.roundLocked);
		round02U.x = round02.x;
		round02U.y = round02.y;

		round03U = new Button("Stage 03 Button U", Resources.roundLocked);
		round03U.x = round03.x;
		round03U.y = round03.y;

		round04U = new Button("Stage 04 Button U", Resources.roundLocked);
		round04U.x = round04.x;
		round04U.y = round04.y;

		round05U = new Button("Stage 05 Button U", Resources.roundLocked);
		round05U.x = round05.x;
		round05U.y = round05.y;
		
		backButton = new Button("Back Button", Resources.backButton[0],Resources.backButton[1]);
		backButton.x = 10;
		backButton.y = 10;
		backButton.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.backSound);
				setState(MainAppListener.STAGE_SELECTION);
			}

		};
		
		round02U.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.lockedSound);
			}

		};
		round03U.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.lockedSound);
			}

		};
		round04U.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.lockedSound);
			}

		};
		round05U.clickListener = new ClickListener() {

			@Override
			public void clicked(Button arg0) {
				// TODO Auto-generated method stub
				Resources.playSound(Resources.lockedSound);
			}

		};

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Stage 1", "Dispose");
		stage.dispose();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Stage 1", "Hide");
//		dispose();
		loading = false;
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Stage 1", "Pause");
	}

	@Override
	public void render(float arg0) {
		// TODO Auto-generated method stub
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears
													// the screen.
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		
		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30.0f));
		stage.draw();
		
		batch.begin();
		
		if (Settings.roundsUnlocked[currentStage][1]){
			int i = Settings.ranks[currentStage][1];
			if (i!=0)
				batch.draw(Resources.medal[i],round01.x - 32, round01.y + 64);

		}
		if (Settings.roundsUnlocked[currentStage][2]){
			int i = Settings.ranks[currentStage][2];
			if (i!=0)
				batch.draw(Resources.medal[i],round02.x - 32, round02.y + 64);

		}
		if (Settings.roundsUnlocked[currentStage][3]){
			int i = Settings.ranks[currentStage][3];
			if (i!=0)
				batch.draw(Resources.medal[i],round03.x - 32, round03.y + 64);

		}
		if (Settings.roundsUnlocked[currentStage][4]){
			int i = Settings.ranks[currentStage][4];
			if (i!=0)
				batch.draw(Resources.medal[i],round04.x - 32, round04.y + 64);

		}
		if (Settings.roundsUnlocked[currentStage][5]){
			int i = Settings.ranks[currentStage][5];
			if (i!=0)
				batch.draw(Resources.medal[i],round05.x - 32, round05.y + 64);

		}
		batch.end();
		

	
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub
//		Gdx.app.log("Stage 1", "Resize");
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Stage 1", "Resume");
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
//		Gdx.app.log("Stage 1", "Show");
		
		Gdx.input.setCatchBackKey(false);
		
		camera.position.set(240, 160, 0);
		
		
		if (!Resources.bgmOpening.isPlaying())
			Resources.playMusic(Resources.bgmOpening);
		
//		if (currentStage == 1){
//			if (round01.unpressedRegion!= Resources.round01Region[currentStage]){
//				round01.unpressedRegion.setRegion(Resources.round0101Region);
//				round02.unpressedRegion.setRegion(Resources.round0102Region);
//				round03.unpressedRegion.setRegion(Resources.round0103Region);
//				round04.unpressedRegion.setRegion(Resources.round0104Region);
//				round05.unpressedRegion.setRegion(Resources.round0105Region);
//				
//				round01.pressedRegion.setRegion(Resources.round0101Region2);
//				round02.pressedRegion.setRegion(Resources.round0102Region2);
//				round03.pressedRegion.setRegion(Resources.round0103Region2);
//				round04.pressedRegion.setRegion(Resources.round0104Region2);
//				round05.pressedRegion.setRegion(Resources.round0105Region2);
//				
//			}
//		}else if (currentStage == 2){
//			if (round01.unpressedRegion!= Resources.round0201Region){
//				round01.unpressedRegion.setRegion(Resources.round0201Region);
//				round02.unpressedRegion.setRegion(Resources.round0202Region);
//				round03.unpressedRegion.setRegion(Resources.round0203Region);
//				round04.unpressedRegion.setRegion(Resources.round0204Region);
//				round05.unpressedRegion.setRegion(Resources.round0205Region);
//				
//				round01.pressedRegion.setRegion(Resources.round0201Region2);
//				round02.pressedRegion.setRegion(Resources.round0202Region2);
//				round03.pressedRegion.setRegion(Resources.round0203Region2);
//				round04.pressedRegion.setRegion(Resources.round0204Region2);
//				round05.pressedRegion.setRegion(Resources.round0205Region2);
//				
//			}
//		}else if (currentStage == 3){
//			if (round01.unpressedRegion!= Resources.round0301Region){
//				round01.unpressedRegion.setRegion(Resources.round0301Region);
//				round02.unpressedRegion.setRegion(Resources.round0302Region);
//				round03.unpressedRegion.setRegion(Resources.round0303Region);
//				round04.unpressedRegion.setRegion(Resources.round0304Region);
//				round05.unpressedRegion.setRegion(Resources.round0305Region);
//				
//				round01.pressedRegion.setRegion(Resources.round0301Region2);
//				round02.pressedRegion.setRegion(Resources.round0302Region2);
//				round03.pressedRegion.setRegion(Resources.round0303Region2);
//				round04.pressedRegion.setRegion(Resources.round0304Region2);
//				round05.pressedRegion.setRegion(Resources.round0305Region2);
//			}
//		}else if (currentStage == 4){
//			if (round01.unpressedRegion!= Resources.round0401Region){
//				round01.unpressedRegion.setRegion(Resources.round0401Region);
//				round02.unpressedRegion.setRegion(Resources.round0402Region);
//				round03.unpressedRegion.setRegion(Resources.round0403Region);
//				round04.unpressedRegion.setRegion(Resources.round0404Region);
//				round05.unpressedRegion.setRegion(Resources.round0405Region);
//				
//				round01.pressedRegion.setRegion(Resources.round0401Region2);
//				round02.pressedRegion.setRegion(Resources.round0402Region2);
//				round03.pressedRegion.setRegion(Resources.round0403Region2);
//				round04.pressedRegion.setRegion(Resources.round0404Region2);
//				round05.pressedRegion.setRegion(Resources.round0405Region2);
//				
//			}
//		}else if (currentStage == 5){
//			if (round01.unpressedRegion!= Resources.round0501Region){
//				round01.unpressedRegion.setRegion(Resources.round0501Region);
//				round02.unpressedRegion.setRegion(Resources.round0502Region);
//				round03.unpressedRegion.setRegion(Resources.round0503Region);
//				round04.unpressedRegion.setRegion(Resources.round0504Region);
//				round05.unpressedRegion.setRegion(Resources.round0505Region);
//				
//				round01.pressedRegion.setRegion(Resources.round0501Region2);
//				round02.pressedRegion.setRegion(Resources.round0502Region2);
//				round03.pressedRegion.setRegion(Resources.round0503Region2);
//				round04.pressedRegion.setRegion(Resources.round0504Region2);
//				round05.pressedRegion.setRegion(Resources.round0505Region2);
//				
//			}
//		}
		
		if (currentStage ==1){
			if (round01.unpressedRegion!= Resources.stage01Region[1]){
				round01.unpressedRegion.setRegion(Resources.stage01Region[1]);
				round02.unpressedRegion.setRegion(Resources.stage01Region[2]);
				round03.unpressedRegion.setRegion(Resources.stage01Region[3]);
				round04.unpressedRegion.setRegion(Resources.stage01Region[4]);
				round05.unpressedRegion.setRegion(Resources.stage01Region[5]);
				
				round01.pressedRegion.setRegion(Resources.stage01Region2[1]);
				round02.pressedRegion.setRegion(Resources.stage01Region2[2]);
				round03.pressedRegion.setRegion(Resources.stage01Region2[3]);
				round04.pressedRegion.setRegion(Resources.stage01Region2[4]);
				round05.pressedRegion.setRegion(Resources.stage01Region2[5]);
			}
		}else if (currentStage ==2){
			if (round01.unpressedRegion!= Resources.stage02Region[1]){
				round01.unpressedRegion.setRegion(Resources.stage02Region[1]);
				round02.unpressedRegion.setRegion(Resources.stage02Region[2]);
				round03.unpressedRegion.setRegion(Resources.stage02Region[3]);
				round04.unpressedRegion.setRegion(Resources.stage02Region[4]);
				round05.unpressedRegion.setRegion(Resources.stage02Region[5]);
				
				round01.pressedRegion.setRegion(Resources.stage02Region2[1]);
				round02.pressedRegion.setRegion(Resources.stage02Region2[2]);
				round03.pressedRegion.setRegion(Resources.stage02Region2[3]);
				round04.pressedRegion.setRegion(Resources.stage02Region2[4]);
				round05.pressedRegion.setRegion(Resources.stage02Region2[5]);
			}
		}else if (currentStage ==3){
			if (round01.unpressedRegion!= Resources.stage03Region[1]){
				round01.unpressedRegion.setRegion(Resources.stage03Region[1]);
				round02.unpressedRegion.setRegion(Resources.stage03Region[2]);
				round03.unpressedRegion.setRegion(Resources.stage03Region[3]);
				round04.unpressedRegion.setRegion(Resources.stage03Region[4]);
				round05.unpressedRegion.setRegion(Resources.stage03Region[5]);
				
				round01.pressedRegion.setRegion(Resources.stage03Region2[1]);
				round02.pressedRegion.setRegion(Resources.stage03Region2[2]);
				round03.pressedRegion.setRegion(Resources.stage03Region2[3]);
				round04.pressedRegion.setRegion(Resources.stage03Region2[4]);
				round05.pressedRegion.setRegion(Resources.stage03Region2[5]);
			}
		}else if (currentStage ==4){
			if (round01.unpressedRegion!= Resources.stage04Region[1]){
				round01.unpressedRegion.setRegion(Resources.stage04Region[1]);
				round02.unpressedRegion.setRegion(Resources.stage04Region[2]);
				round03.unpressedRegion.setRegion(Resources.stage04Region[3]);
				round04.unpressedRegion.setRegion(Resources.stage04Region[4]);
				round05.unpressedRegion.setRegion(Resources.stage04Region[5]);
				
				round01.pressedRegion.setRegion(Resources.stage04Region2[1]);
				round02.pressedRegion.setRegion(Resources.stage04Region2[2]);
				round03.pressedRegion.setRegion(Resources.stage04Region2[3]);
				round04.pressedRegion.setRegion(Resources.stage04Region2[4]);
				round05.pressedRegion.setRegion(Resources.stage04Region2[5]);
			}
		}else if (currentStage ==5){
			if (round01.unpressedRegion!= Resources.stage05Region[1]){
				round01.unpressedRegion.setRegion(Resources.stage05Region[1]);
				round02.unpressedRegion.setRegion(Resources.stage05Region[2]);
				round03.unpressedRegion.setRegion(Resources.stage05Region[3]);
				round04.unpressedRegion.setRegion(Resources.stage05Region[4]);
				round05.unpressedRegion.setRegion(Resources.stage05Region[5]);
				
				round01.pressedRegion.setRegion(Resources.stage05Region2[1]);
				round02.pressedRegion.setRegion(Resources.stage05Region2[2]);
				round03.pressedRegion.setRegion(Resources.stage05Region2[3]);
				round04.pressedRegion.setRegion(Resources.stage05Region2[4]);
				round05.pressedRegion.setRegion(Resources.stage05Region2[5]);
			}
		}
		

		round01Unlocked = Settings.roundsUnlocked[currentStage][1];
		round02Unlocked = Settings.roundsUnlocked[currentStage][2];
		round03Unlocked = Settings.roundsUnlocked[currentStage][3];
		round04Unlocked = Settings.roundsUnlocked[currentStage][4];
		round05Unlocked = Settings.roundsUnlocked[currentStage][5];

		stage.addActor(bg);
		
		stage.addActor(round01);
		stage.addActor(backButton);

//		round01.action(FadeIn.$(1f));
//		backButton.action(FadeIn.$(1f));

		if (round02Unlocked) {
			stage.addActor(round02);
//			round02.action(FadeIn.$(1f));
		} else {
			stage.addActor(round02U);
//			round02U.action(FadeIn.$(1f));
		}

		if (round03Unlocked) {
			stage.addActor(round03);
//			round03.action(FadeIn.$(1f));
		} else {
			stage.addActor(round03U);
//			round03U.action(FadeIn.$(1f));
		}

		if (round04Unlocked) {
			stage.addActor(round04);
//			round04.action(FadeIn.$(1f));
		} else {
			stage.addActor(round04U);
//			round04U.action(FadeIn.$(1f));
		}

		if (round05Unlocked) {
			stage.addActor(round05);
//			round05.action(FadeIn.$(1f));
		} else {
			stage.addActor(round05U);
//			round05U.action(FadeIn.$(1f));
		}
		Gdx.input.setInputProcessor(this);
		
//		bg.action(FadeIn.$(1f).setCompletionListener(new OnActionCompleted(){
//
//			@Override
//			public void completed(Action arg0) {
//				// TODO Auto-generated method stub
//				setInputProcessor();
//			}
//			
//		}));
		
//		Gdx.input.setInputProcessor(this);
	}

	@Override
	public boolean keyDown(int arg0) {
		// TODO Auto-generated method stub
		
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		if (arg0 == Input.Keys.BACK) {
			Resources.playSound(Resources.backSound);
			setState(MainAppListener.STAGE_SELECTION);
			return false;
		}
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {

		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {

		return stage.touchDown(arg0, arg1, arg2, arg3);
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {

		return stage.touchDragged(arg0, arg1, arg2);
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {

		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		return stage.touchUp(arg0, arg1, arg2, arg3);
	}

	private void setState(int state) {
		mainListener.setState(state);
	}

	public void startLoading( int round){
		Screen loading = new LoadingScreen(this,round);
		mainListener.setScreen(loading);
	}
	
	public void startBattle(int round) {
		mainListener.startBattle(currentStage, round, this);
	}
	
	public void setInputProcessor(){
		
	}

}
